"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 硬件故障类--磁盘
Case Name   : 磁盘满时备升主
Create At   : 2021/07/13
@zou_jialiang0505328126
Description :
    1. 主节点注入磁盘满故障
    2.主节点创建表并插入数据
    3.备节点注入磁盘满故障
    4.备节点升主
    5.备节点清除故障
    6.备节点升主
Expect      :
    1.成功
    2.失败
    3.成功
    4.失败
    5.成功
    6.成功
History     :
    2021/9/22 @wan005:优化断言信息
"""

import unittest
from yat.test import Node, macro
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import RestartDbCluster
from testcase.utils.Logger import Logger



class CPUOverloadAndRunTPCC(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info(
            '---------Opengauss_Reliability_Hardware_Case045:开始执行---------')
        self.pri_root_node = Node(node='PrimaryRoot')
        self.sta1_root_node = Node(node='Standby1Root')
        self.com = Common()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta1_sh = CommonSH('Standby1DbUser')
        self.constant = Constant()

        self.log.info("-----------获取磁盘名 -----------")
        cmd = f"df -h {macro.DB_INSTANCE_PATH}"
        self.log.info(cmd)
        msg = self.sta1_root_node.sh(cmd).result()
        self.log.info(msg)
        self.sta1_disk_name = msg.splitlines()[-1].split()[0].strip()
        msg = self.pri_root_node.sh(cmd).result()
        self.log.info(msg)
        self.pri_disk_name = msg.splitlines()[-1].split()[0].strip()
        self.tb_name = "disk_tb"

    def test_main(self):
        self.log.info("--------------1.主节点注入磁盘满故障-------------")
        inject_command = f"rfile_full (diskname) " \
            f"values ({self.pri_disk_name})"
        result = self.com.cfe_inject(self.pri_root_node, inject_command)
        self.log.info(result)
        flg = "successful execution" in result or \
              self.constant.FAILED_CONNECT_DB in result
        self.assertTrue(flg)

        self.log.info("----------------2.创建表并插入数据-------------")
        sql = f"create table {self.tb_name}(i int);" \
            f"insert into {self.tb_name} values(generate_series(1,9000));"
        result = self.pri_sh.executDbSql(sql)
        self.log.info(result)
        self.assertIn(self.constant.NO_SPACE_BUILD_FAIL_MSG, result)

        self.log.info("--------------3.备节点注入磁盘满故障-------------")
        inject_command = f"rfile_full (diskname) " \
            f"values ({self.sta1_disk_name})"
        result = self.com.cfe_inject(self.sta1_root_node, inject_command)
        self.log.info(result)
        flg = "successful execution" in result or \
              self.constant.FAILED_CONNECT_DB in result
        self.assertTrue(flg)

        self.log.info("------------4.备节点升主----------------")
        result = self.sta1_sh.executeGsctl('failover',
                                          self.constant.FAILOVER_SUCCESS_MSG)
        self.assertFalse(result)

        self.log.info("--------------5.备节点清理磁盘满故障-------------")
        clean_command = f"rfile_full  where(diskname={self.sta1_disk_name})"
        result = self.com.cfe_clean(self.sta1_root_node, clean_command)
        self.log.info(result)
        self.assertIn("successful execution", result)

        self.log.info("-------------6.备升主---------------")
        result = self.sta1_sh.executeGsctl('failover',
                                          self.constant.FAILOVER_SUCCESS_MSG)
        self.assertTrue(result)

    def tearDown(self):
        self.log.info("--------------主节点清理磁盘满故障-------------")
        clean_command = f"rfile_full  where(diskname={self.pri_disk_name})"
        result = self.com.cfe_clean(self.pri_root_node, clean_command)
        self.log.info(result)
        self.log.info("--------------备节点清理磁盘满故障-------------")
        clean_command = f"rfile_full  where(diskname={self.sta1_disk_name})"
        result = self.com.cfe_clean(self.sta1_root_node, clean_command)
        self.log.info(result)
        self.log.info("---------------重启集群------------")
        self.pri_sh.stopDbCluster()
        self.pri_sh.startDbCluster()
        self.sta1_sh.executeGsctl("build", self.constant.REBUILD_SUCCESS_MSG)

        self.log.info("------------删除表-----------")
        sql = f"drop table if exists {self.tb_name};"
        result = self.pri_sh.executDbSql(sql)
        self.log.info(result)
        self.log.info('-----Opengauss_Reliability_Hardware_Case045:执行完成-----')
